package MyDisplayObjects
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/** An object of this class places a group of tabs in a rectangular array */
	public class RectangularTabMenu extends MovieClip
	{
		// private vars
		//private var tabs:Array;
		private var tabWidth:Number, tabHeight:Number;
		private var _selectedTabName:String;
		
		public function get selectedTabName ():String{ return _selectedTabName; }
		
		public function RectangularTabMenu(width:Number, height:Number, tabNames:Array, columns:int, rows:int)
		{
			this.graphics.drawRect(0, 0, width, height);
			tabWidth = width/columns;
			tabHeight = height/rows;
			var maxCharString:String = "";
			for each (var s:String in tabNames)
			{
				if (s.length > maxCharString.length) maxCharString = s;
			}
			for (var i:uint=0; i < tabNames.length; i++)
			{
				var tab:TabButton = new TabButton (tabWidth, tabHeight, "North", "Rectangle", tabHeight, tabNames[i], 0xFFCCFF, maxCharString);
				tab.name = tabNames[i];
				tab.x = i%columns*tabWidth;
				tab.y = Math.floor(i/columns)*tabHeight;
				tab.addEventListener(MouseEvent.CLICK, handleClick);
				this.addChild(tab);
			}
		}
		
		private function handleClick (event:MouseEvent):void
		{
			_selectedTabName = TabButton(event.currentTarget).name;
			this.dispatchEvent(new Event(Event.COMPLETE));	
		}
	}
}